<?php
/** 
 * 弹幕的操作
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

namespace Api\Event;

class BarrageEvent extends BaseEvent 
{	
	/**
	*Mongo集合的实例化
	*/
	protected $serviceObj = '';
	/**
	*参数初始化
	*/
	protected function _initialize(){
		parent::_initialize();
		$this->serviceObj = D('Common/Barrage', 'Service');
	}

	/** 
	 * 对video进行添加弹幕
	 * @author Legend. <xcx_legender@qq.com>
	 * @param int $video_id 视频ID
	 * @param array $user 用户信息
	 * @param string $data 弹幕的内容
	 * @return 
	*/
	public function add($video_id, $user, $data)
	{
		$save['video_id'] = intval($video_id);
		$save['uid']      = intval($user['uid']);
		$save['text']     = $data['text'];
		$save['startTime']     = $data['startTime'];
		$save['aliveTime']     = $data['aliveTime'];
		$save['textColor']     = $data['textColor'];

		if ($save['text'] === '')
		{
			$this->error['code'] = self::$API_ERROR['BARRAGE_TEXT']; 
			$this->error['info'] = 'text null'; 
			return false;
		}

		if (!$video_id)
		{
			$this->error['code'] = self::$API_ERROR['VIDEO']; 
			$this->error['info'] = 'error video'; 
			return false;
		}

		if (!$save['uid'])
		{
			$this->error['code'] = self::$API_ERROR['NO_USER']; 
			$this->error['info'] = 'error user'; 
			return false;
		}
		$save = $this->serviceObj->create($save);
		if ( $_id = $this->serviceObj->add($save) )
		{
			$where['id'] = $_id;//new \MongoId($_id);
			$barrage =  $this->serviceObj->where($where)->find();
			return $barrage;
		}
		else
		{
			$this->error['code'] = self::$API_ERROR['ERROR_BARRAGE'];
			$this->error['info'] = $this->serviceObj->getError(); 
			return false;
		}
	}

	
	 /** 
	  * 根据视频标识$vid获取其所有的弹幕信息
	  * @author Legend. <xcx_legender@qq.com>
	  * @param 
	  * @return 
	  */
	 
	public function lists($video_id){
 		$map['video_id'] = intval($video_id);
 		$map['status']   = array('egt', 0);
 		$lists = $this->serviceObj->where($map)->select();
 		return $lists;
	}


}